package GameTools;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;

public class ydSprite {
	
	public static Context context;
	
	Paint p =new Paint();
	
	public Bitmap sprite;
	Matrix m;
	
	public ydPoint pivotPoint;
	
	public float scale=0.5f;
	
	public ydSprite(int imgID, ydPoint scale, ydPoint pivotPoint){
		InputStream is = context.getResources().openRawResource(imgID);
		sprite = BitmapFactory.decodeStream(is);  
		
		/*sprite = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(context.getResources(), imgID),
				(int)scale.x, (int)scale.y, true);*/
		
		this.pivotPoint=new ydPoint(pivotPoint);
		
		p.setColor(Color.BLUE);
		
		
	}
	
	public ydSprite(int imgID, ydPoint scale){
		InputStream is = context.getResources().openRawResource(imgID);
		sprite = BitmapFactory.decodeStream(is);  
		
		/*sprite = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(context.getResources(), imgID),
				(int)scale.x, (int)scale.y, true);*/
		
		this.pivotPoint=new ydPoint(sprite.getHeight()/2, sprite.getHeight()/2);
		
		p.setColor(Color.BLUE);
	}
	
	public ydSprite(int imgID){
		InputStream is = context.getResources().openRawResource(imgID);
		sprite = BitmapFactory.decodeStream(is);  
		
		/*sprite = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(context.getResources(), imgID),
				(int)scale.x, (int)scale.y, true);*/
		
		this.pivotPoint=new ydPoint(scale*sprite.getHeight()/2, scale*sprite.getHeight()/2);
		
		p.setColor(Color.BLUE);
	}
	
	public void draw(Canvas canvas, ydLine at){
		float IS=1-scale;
		m=new Matrix();
		m.setScale(scale, scale);
		m.postTranslate(at.p1.x, at.p1.y-pivotPoint.y);
		m.postRotate((float)at.angle, at.p1.x, at.p1.y);
		
		canvas.drawBitmap(sprite, m, p);
		
		canvas.drawPoint((int)(at.p1.x), (int)(at.p1.y), p);
	}

	public float getWidth() {
		return sprite.getWidth()*scale;
	}

	public float getHeight() {
		return sprite.getHeight()*scale;
	}

	public void setScale(float s) {
		scale=s;
		
	}

	public ydPoint getSize() {
		
		return new ydPoint(getWidth(), getHeight());
	}

}
